package cc.mrbird.febs.buss.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.DocumentHelper;


public class HySendSms {
	private static String userid="16440";
	private static String account="zzgfdz";
	private static String password="123456";
//	private static String userid="16431";
//	private static String account="B2933";
//	private static String password="123456";
	private static String url_send="http://www.duanxin10086.com/sms.aspx?action=send";
	private static String url_query="http://www.duanxin10086.com/callApi.aspx?action=query";
	
	
	/**
	 * 海岩短信平台发送短信
	 * @param mobile 手机号
	 * @param content 短信内容
	 * @param taskName 短信任务描述
	 * @return 返回的是一个map
	 * 	<?xml version="1.0" encoding="utf-8" ?>
	 *	<returnsms>
	 *		<returnstatus>status</returnstatus> ---------- 返回状态值：成功返回Success 失败返回：Faild
	 *		<message>message</message> ---------- 返回信息：见下表
	 *		<remainpoint> remainpoint</remainpoint> ---------- 返回余额
	 *		<taskID>taskID</taskID> ----------- 返回本次任务的序列ID
	 *		<successCounts>successCounts</successCounts> --成功短信数：当成功后返回提交成功短信数
	 *	</returnsms>
	 * @throws Exception
	 */
	public static Map sendMessage(String mobile,String content,String taskName) throws Exception {
		//URL localURL = new URL("http://www.duanxin10086.com/sms.aspx?action=send&userid=16430&account=D2932&password=123456&mobile=15538326939&content=为提升服务质量，请您对[郑东新区政务服务大厅]2019-09-24办理的业务[名称核准]进行评价，非常满意1，满意2，一般3，不满意4，非常不满意5，请回复数字，祝您工作顺利！中心服务热线0371-12345&sendTime=&taskName=本次任务描述11");
		URL localURL = new URL(HySendSms.url_send+"&userid="+HySendSms.userid+"&account="+HySendSms.account+"&password="+HySendSms.password+"&mobile="+mobile+"&content="+content+"&sendTime=&taskName="+taskName);
		System.out.println(HySendSms.url_send+"&userid="+HySendSms.userid+"&account="+HySendSms.account+"&password="+HySendSms.password+"&mobile="+mobile+"&content="+content+"&sendTime=&taskName="+taskName);
		URLConnection connection = localURL.openConnection();
		HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
		/*httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");*/
		System.out.println("请求头");
		httpURLConnection.setRequestProperty("Charsert", "UTF-8");
		httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");//设置参数类型是json格式
		httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
		httpURLConnection.setRequestProperty("logType", "base");
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer resultBuffer = new StringBuffer();
		String tempLine = null;
		// 响应失败
		if (httpURLConnection.getResponseCode() >= 300) {
			throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
		}
		try {
			inputStream = httpURLConnection.getInputStream();
			inputStreamReader = new InputStreamReader(inputStream);
			//reader = new BufferedReader(inputStreamReader);
			reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			while ((tempLine = reader.readLine()) != null) {
				resultBuffer.append(tempLine);
			}
		} finally {
			if (reader != null) {
				reader.close();
			}

			if (inputStreamReader != null) {
				inputStreamReader.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
		Map map=new PaserXmlStr().parse4DOM4j(resultBuffer.toString());
		return map;
	}

	public static void main(String[] args) throws Exception {
		//String content="【大厅差评提醒】大厅工作人员您好，手机号为15538326939的用户对在2019-09-19 15:06:57办理的智汇郑州进行了差评，请及时处理";
		String content="【政务服务】大厅工作人员您好,手机号为15538326939的用户对在2019-09-19办理的智汇郑州进行了差评,请及时处理";
		HySendSms.sendMessage("15538326939",content,"用户差评通知大厅管理人员");
	}

}
